package net.dgg.rz.common.systemRecord.service;

import net.dgg.framework.tac.utils.idutil.DggKeyWorker;
import net.dgg.rz.common.systemRecord.dao.SystemRecordDao;
import net.dgg.rz.common.systemRecord.entity.SystemRecord;
import net.dgg.rz.common.systemRecord.vo.SystemRecordVO;
import net.dgg.rz.common.utils.DateUtils;
import net.dgg.rz.common.utils.StringUtils;
import net.dgg.rz.common.utils.ValidateUtils;
import net.dgg.rz.production.exception.ProductOrderExcption;
import net.dgg.tmd.foundation.platform.org.entity.OrganizationEntity;
import net.dgg.tmd.foundation.platform.org.service.OrganizationManager;
import net.dgg.tmd.foundation.platform.session.SessionManager;
import net.dgg.tmd.foundation.platform.user.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 系统操作日志Service
 *
 * @Author:HDC
 * @Date: Created in 2018/8/14 16:51
 * @Copyright © dgg group.All Rights Reserved. 版权信息
 */
@Service
public class SystemRecordService {

    @Autowired
    private SystemRecordDao systemRecordDao;
    @Autowired
    private SessionManager sessionManager;
    @Autowired
    private OrganizationManager organizationManager;


    /**
     * 保存
     *
     * @param systemRecord 操作日志实体
     */
    @Transactional
    public void save(SystemRecord systemRecord) {
        UserEntity userEntity = sessionManager.getCurrentUser();
        OrganizationEntity operatedOrgan = organizationManager.findOrgEntityByOrgId(userEntity.getOrgId());

        systemRecord.setId(DggKeyWorker.nextId());
        systemRecord.setOperaterId(userEntity.getId());
        systemRecord.setOperaterName(userEntity.getRealName() + userEntity.getLoginName());
        systemRecord.setOperaterOrgId(operatedOrgan.getId());
        systemRecord.setOperaterOrgName(operatedOrgan.getName());

        systemRecord.setOperateTime(new Date());
        systemRecordDao.insert(systemRecord);
    }

    /**
     * 保存
     *
     * @param systemRecord 操作日志实体
     */
    @Transactional
    public void apiSave(SystemRecord systemRecord, UserEntity userEntity) {
        OrganizationEntity operatedOrgan = organizationManager.findOrgEntityByOrgId(userEntity.getOrgId());

        systemRecord.setId(DggKeyWorker.nextId());
        systemRecord.setOperaterId(userEntity.getId());
        systemRecord.setOperaterName(userEntity.getRealName() + userEntity.getLoginName());
        systemRecord.setOperaterOrgId(operatedOrgan.getId());
        systemRecord.setOperaterOrgName(operatedOrgan.getName());

        systemRecord.setOperateTime(new Date());
        systemRecordDao.insert(systemRecord);
    }

    @Transactional
    public void saveRecords(List<SystemRecord> systemRecords) {
        if (null != systemRecords && systemRecords.size() > 0) {
            systemRecordDao.insertRecords(systemRecords);
        }
    }

    /**
     * 保存
     *
     * @param systemRecord 操作日志实体
     * @param beOperated   被操作人
     */
    @Transactional
    public void save(SystemRecord systemRecord, UserEntity beOperated,UserEntity currUser) {
        UserEntity userEntity = new UserEntity();
        if (null == currUser) {
            userEntity=sessionManager.getCurrentUser();
        }else {
            userEntity=currUser;
        }
        ValidateUtils.strNotEmpty(userEntity, ProductOrderExcption.class, "当前用户未登录");
        OrganizationEntity operatedOrgan = organizationManager.findOrgEntityByOrgId(userEntity.getOrgId());
        OrganizationEntity beOperatedOrgan = organizationManager.findOrgEntityByOrgId(beOperated.getOrgId());

        systemRecord.setId(DggKeyWorker.nextId());
        systemRecord.setOperaterId(userEntity.getId());
        systemRecord.setOperaterName(userEntity.getRealName() + userEntity.getLoginName());
        systemRecord.setOperaterOrgId(operatedOrgan.getId());
        systemRecord.setOperaterOrgName(operatedOrgan.getName());

        systemRecord.setBeOperaterId(beOperated.getId());
        systemRecord.setBeOperaterName(beOperated.getRealName() + beOperated.getLoginName());
        systemRecord.setBeOperaterOrgId(beOperatedOrgan.getId());
        systemRecord.setBeOperaterOrgName(beOperatedOrgan.getName());
        systemRecord.setOperateTime(new Date());
        systemRecordDao.insert(systemRecord);
    }

    /**
     * 查询所有日志信息
     *
     * @param params
     * @return
     */
    public List<SystemRecord> findAllWithPage(Map params) {
        if (null == params.get("orderColumn")) {
            params.put("orderColumn", "operate_time");
            params.put("orderDir", "desc");
        }
        return systemRecordDao.findAllWithPage(params);
    }

    /**
     * <p>@Description 根据条件查日志信息</p>
     * <p>@author mqy</p>
     * <p>@Time 2018/8/23 0023 11:04 创建时间</p>
     * <p>@return</p>
     */
    public List<SystemRecord> findByParamsWithPage(Map params) {
        return systemRecordDao.findByParamsWithPage(params);
    }

    /**
     * 根据条件查询记录-导出
     *
     * @param params
     * @return
     */
    public List<SystemRecordVO> findByParamsList(Map params) {
        List<SystemRecordVO> voList = new ArrayList<>();
        List<SystemRecord> srList = systemRecordDao.findByParamsList(params);
        for (SystemRecord record : srList) {
            SystemRecordVO vo = new SystemRecordVO();
            vo.setWorkNo(record.getWorkNo());
            vo.setSignOrgName(record.getSignOrgName());
            vo.setOperaterName(record.getOperaterName());
            vo.setOperaterOrgName(record.getOperaterOrgName());
            vo.setBeOperaterName(record.getBeOperaterName());
            vo.setBeOperaterOrgName(record.getBeOperaterOrgName());
            vo.setOperateTime(DateUtils.dateString(record.getOperateTime(), "yyyy-MM-dd HH:mm:ss"));
            vo.setOperateType(record.getOperateType());
            vo.setRemarks(record.getRemarks());
            voList.add(vo);
        }
        return voList;
    }

    /**
     * 查询证书操作日志信息
     *
     * @param params
     * @return
     */
    public List<SystemRecord> findCredentialWithPage(Map params) {
        if (StringUtils.isEmpty("") && StringUtils.isEmpty("")) {
            params.put("orderColumn", "operate_time");
            params.put("orderDir", "desc");
        }
        params.put("source", "1");
        return systemRecordDao.findAllWithPage(params);
    }

}
